package com.jincheng.system.service.impl;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.jincheng.common.utils.sign.Base64;
import com.jincheng.system.service.IQrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Service
public class QrCodeServiceImpl implements IQrCodeService {

    private final QrConfig qrConfig;

    public QrCodeServiceImpl(QrConfig qrConfig) {
        this.qrConfig = qrConfig;
    }

    @Override
    public String createCodeToStream(String content) {
        BufferedImage image  = QrCodeUtil.generate(content, qrConfig);

        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Base64.encode(os.toByteArray());
    }

    @Override
    public void createCodeToFile(String content, String filePath) {

    }
}
